package com.example.content.viewmodule

import com.example.content.DO_ACTION
import com.example.content.manager.GlobalMgr
import com.example.content.repository.RepositoryMgr
import com.example.content.repository.bean.*
import com.example.content.viewmodule.viewdata.CommonViewState
import com.example.core.livedata.SingleLiveEvent

/**
 * Created by zhudongao
 * Desc
 * Date 1/6/21
 */
open class BaseFuncViewModel : BaseViewModel<CommonViewState>() {

    val productInfoResult = SingleLiveEvent<List<ProductInfo>>()
    val userInfoResult = SingleLiveEvent<List<UserInfo>>()
    val recordInfoResult = SingleLiveEvent<List<RewardRecord>>()

    /**
     * 关注、取消关注
     */
    fun follow(followReq: FollowReq) {
        launch(
            block = {
                val result = RepositoryMgr.follow(followReq)
                GlobalMgr.follow.postValue(result.apiData())
            }
        )
    }

    /**
     * 获取用户的打赏记录
     */
    fun getUserRewardInfo(createId: Int, offset: Int) {
        launch(
            block = {
                RepositoryMgr.getUserRewardInfo(createId, offset)
            }
        )
    }

    /**
     * 点赞, 收藏
     */
    fun userAction(userActionReq: UserActionReq) {
        launch(
            block = {
                val result = RepositoryMgr.userAction(userActionReq)
                if (result.code == 200) {
                    GlobalMgr.collect.postValue(Triple(userActionReq.creation_id, userActionReq.action_type == DO_ACTION, System.currentTimeMillis()))
                    GlobalMgr.thumbUp.postValue(true)
                }
            }
        )
    }

    /**
     * 获取作品信息By id
     */
    fun getProductInfoById(idList: List<Int>) {
        launch(
            block = {
                productInfoResult.postValue(RepositoryMgr.getProductInfoById(idList).apiData())
            }
        )
    }

    /**
     * 删除作品
     */
    fun delProduct(createId: Int) {
        launch(
            block = {
                RepositoryMgr.delProductById(createId)
            }
        )
    }

    /**
     * 查看作品根据id
     */
    fun viewProductById(createId: Int) {
        launch(
            block = {
                RepositoryMgr.viewProductById(createId)
            }
        )
    }

    /**
     * 获取作品的查看、收藏、点赞的用户
     */
    fun getProductActionUser(creationId: Int, action: Int) {
        launch(
            block = {
                userInfoResult.postValue(
                    RepositoryMgr.getProductActionUser(creationId, action).apiData()
                )
            }
        )
    }

    //打赏记录
    fun rewardRecordInfo(creationId: Int, coin: Int) {
        launch(block = {
            RepositoryMgr.rewardProduct(creationId, coin)
        })
    }

    /**
     * 获得作品的打赏记录
     */
    fun getRewardRecordInfo(creationId: Int) {
        launch(
            block = {
                recordInfoResult.postValue(RepositoryMgr.getRewardRecordInfo(creationId).apiData())
            }
        )
    }
}